﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Reflection;
using System.Diagnostics;

namespace NLayers.Setup.CustomAction
{
    /// <summary>
    /// Action Utility class for both Install and Unistall
    /// </summary>
    public class ActionUtils
    {
        public const string VisualStudioRegistryKey = @"SOFTWARE\Microsoft\VisualStudio\10.0\Setup\VS";

        public string ApplicationPath
        {
            get;
            set;
        }

        public string VisualStudioPath
        {
            get;
            set;
        }

        public string ProjectTemplatesPath
        {
            get;
            set;
        }

        public string ItemTemplatesPath
        {
            get;
            set;
        }
        
        public ActionUtils()
        {
            SetPaths();
        }

        public void SetPaths()
        {
            ApplicationPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            VisualStudioPath = GetVSPath();                        // e:\\Program Files\\Microsoft Visual Studio 10.0\\
            ProjectTemplatesPath = VisualStudioPath + @"Common7\IDE\ProjectTemplates\NLayers\";
            ItemTemplatesPath = VisualStudioPath + @"Common7\IDE\ItemTemplates\CSharp\NLayers\";
        }

        public void CreateFolders()
        {
            if (!Directory.Exists(ProjectTemplatesPath))
                Directory.CreateDirectory(ProjectTemplatesPath);

            if (!Directory.Exists(ItemTemplatesPath))
                Directory.CreateDirectory(ItemTemplatesPath);
        }

        private string GetVSPath()
        {
            RegistryKey regKey = Registry.LocalMachine.OpenSubKey(VisualStudioRegistryKey);
            string result = regKey.GetValue("ProductDir").ToString();
            regKey.Close();

            return result;
        }

        public void DeleteFolders()
        {
            if (Directory.Exists(ItemTemplatesPath))
                Directory.Delete(ItemTemplatesPath, true);

            if (Directory.Exists(ProjectTemplatesPath))
                Directory.Delete(ProjectTemplatesPath, true);
        }

        /// <summary>
        /// Execute command: devenv /installvstemplates
        /// </summary>
        /// <param name="vsPath"></param>
        /// <returns></returns>
        public bool ExecuteInstallVSTemplatesCommand()
        {
            string commandPath = VisualStudioPath + @"Common7\IDE\devenv.com";

            if (!File.Exists(commandPath))
            {
                Console.WriteLine("Unable to find devenv.com" + Environment.NewLine + "Path: " + commandPath);
                Console.WriteLine("Press any key to continue..");
                Console.ReadKey(false);

                return false;
            }
            else
            {
                Process process = Process.Start(commandPath, " /installvstemplates");
                process.WaitForExit();

                return true;
            }
        }
    }
}
